let currentIndex = 0;
let allCourses = [];

async function fetchData(url) {
    try {
        const response = await fetch(url);
        if (!response.ok) {
            throw new Error('网络请求错误');
        }
        const data = await response.json();
        return data;
    } catch (error) {
        console.error('返回错误:', error);
        return null;
    }
}

async function mergeCourseData() {
    const url = 'http://82.156.191.173:3000/data';

    const data = await fetchData(url);

    if (data) {
        allCourses = allCourses.concat(data);
    }

    console.log(allCourses);

    views(allCourses);
    titles(allCourses);
    clasnum(allCourses);
    clasnam(allCourses);
    coverurl(allCourses);
    bvid(allCourses);
    videotitleurl(allCourses);
    indexlist(allCourses);
    indexlisturl(allCourses);
}

// api数据获取与合并结束

//下为api数据调用函数
//播放量
async function views(allCourses) {
    const views = document.querySelectorAll('.viewmark');
    for (let i = 0; i < views.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        views[i].textContent = allCourses[dataIndex].view;
    }
}
//标题
async function titles(allCourses) {
    const titles = document.querySelectorAll('.titlemark');
    for (let i = 0; i < titles.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        titles[i].textContent = allCourses[dataIndex].course_name;
    }
}
//集数
async function clasnum(allCourses) {
    const clasnum = document.querySelectorAll('.clasnummark');
    for (let i = 0; i < clasnum.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        clasnum[i].textContent = allCourses[dataIndex].current_num;
    }
}
//分类
async function clasnam(allCourses) {
    const clasnam = document.querySelectorAll('.clasnammark');
    for (let i = 0; i < clasnam.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        clasnam[i].textContent = allCourses[dataIndex].category_name;
    }
}
//封面图
async function coverurl(allCourses) {
    const coverurl = document.querySelectorAll('.coverurlmark');
    for (let i = 0; i < coverurl.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        coverurl[i].src = allCourses[dataIndex].cover;
    }
}
//封面超链接
async function bvid(allCourses) {
    const coverurl = document.querySelectorAll('.videomark');
    for (let i = 0; i < coverurl.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        const baseUrl = 'https://www.bilibili.com/video/';
        coverurl[i].href = baseUrl + allCourses[dataIndex].bvid;
    }
}
//标题超链接
async function videotitleurl(allCourses) {
    const videotitleurl = document.querySelectorAll('.videotitlemark');
    for (let i = 0; i < videotitleurl.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        const baseUrl = 'https://www.bilibili.com/video/';
        videotitleurl[i].href = baseUrl + allCourses[dataIndex].bvid;
    }
}
//索引列表
async function indexlist(allCourses) {
    const indexlist = document.querySelectorAll('.indexlistmark');
    for (let i = 0; i < indexlist.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        indexlist[i].textContent = allCourses[dataIndex].course_name;
    }
}
//列表超链接
async function indexlisturl(allCourses) {
    const indexlisturl = document.querySelectorAll('.indexlisturlmark');
    for (let i = 0; i < indexlisturl.length; i++) {
        const dataIndex = (currentIndex + i) % allCourses.length;
        const baseUrl = 'https://www.bilibili.com/video/';
        indexlisturl[i].href = baseUrl + allCourses[dataIndex].bvid;
    }
}

// 下为刷新

document.getElementById('f5').addEventListener('click', () => {
    currentIndex += 6;
    views(allCourses);
    titles(allCourses);
    clasnum(allCourses);
    clasnam(allCourses);
    coverurl(allCourses);
    bvid(allCourses);
    videotitleurl(allCourses);
    indexlist(allCourses);
    indexlisturl(allCourses);
});

mergeCourseData();
